<?php

namespace App\Http\Controllers\Admin;

use app\console\models\Count;
use App\Models\Coupon;
use App\Models\CouponRule;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CouponController extends Controller
{
    /**优惠券列表页面
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function rule(Request $request,CouponRule $ruleModel)
    {

        $data = CouponRule::query()->where(['disabled'=>'0'])->get()->toArray();
        return view('admin.coupon.rule',['list'=>$data,'ruleModel'=>$ruleModel]);
    }

    /**优惠券规则的创建页面
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create_rule_form(Request $request)
    {
        $data = [
            'action' => 'create'
        ];
        return view('admin.coupon.create_rule_form',$data);
    }

    /**优惠券规则的创建
     * @param Request $request
     * @param CouponRule $couponRuleModel
     * @return \Illuminate\Http\RedirectResponse
     */
    public function createrule(Request $request,CouponRule $couponRuleModel)
    {
        if($couponRuleModel -> save_rule($request->all())){
            return redirect('admin/coupon/rule')->with('success','添加规则成功');
        }
        return redirect()->back()->with('danger',$couponRuleModel->error);
    }

    /**优惠券规则的编辑页面
     * @param Request $request
     * @param CouponRule $couponRuleModel
     * @return \Illuminate\Http\RedirectResponse
     */
    public function edit_rule_form(Request $request,CouponRule $couponRuleModel,$rule_id=0)
    {
        if(empty($rule_id)){
            return redirect('admin/coupon/rule');
        }

        $data['rule_row'] = CouponRule::query()->find($rule_id);
        $data['condition'] = $data['rule_row']['conditions']?unserialize($data['rule_row']['conditions']):[];
        $data['actions'] = $data['rule_row']['actions']?unserialize($data['rule_row']['actions']):[];
        $data['action'] = 'edit';

        return view('admin.coupon.edit_rule_form',$data);
    }

    /**优惠券规则的编辑
     * @param Request $request
     * @param CouponRule $couponRuleModel
     * @return \Illuminate\Http\RedirectResponse
     */
    public function editrule(Request $request,CouponRule $couponRuleModel)
    {
        $post_data = $request -> all();
        if(!$couponRuleModel -> save_rule($post_data)){
            return redirect()->back()->with('danger',$couponRuleModel->error);
        }
        return redirect('admin/coupon/rule')->with('success','编辑规则成功');
    }

    /**优惠券规则的删除
     * @param Request $request
     * @param CouponRule $couponRule
     */
    public function delterule(Request $request,CouponRule $couponRule,$rule_id)
    {
        if(empty($rule_id)){
            return redirect('admin/coupon/rule')->with('danger','规则id不能为空');
        }
        $rule_model = $couponRule->newQuery()->find($rule_id);
        if(!empty($rule_model)){
            $rule_model -> disabled = 1;
            $rule_model -> save();
        }

        return redirect('admin/coupon/rule')->with('success','删除成功');
    }

    /**优惠券的列表页面
     * @param Request $request
     * @param Coupon $couponModel
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function coupon_list(Request $request,Coupon $couponModel)
    {
        $data['lists'] =  $couponModel -> where('disabled',0)->get()->toArray();
        return view('admin.coupon.coupon',$data);
    }

    /**优惠券的创建页面
     * @param Request $request
     * @param Coupon $couponModel
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create_coupon_form(Request $request,Coupon $couponModel)
    {
        $data['rule_list'] = CouponRule::query()->where(['disabled'=>'0'])->get()->toArray();
        $data['action'] = 'create';
        return view('admin.coupon.create_coupon_form',$data);
    }

    /**优惠券的编辑页面
     * @param Request $request
     * @param Coupon $couponModel
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit_coupon_form(Request $request,Coupon $couponModel,$coupon_id=0)
    {
        $data['rule_list'] = CouponRule::query()->where(['disabled'=>'0'])->get()->toArray();
        $data['action'] = 'edit';
        $data['coupon_row'] = Coupon::find($coupon_id);
        return view('admin.coupon.edit_coupon_form',$data);
    }

    /**优惠券的创建
     * @param Request $request
     * @param Coupon $couponModel
     * @return \Illuminate\Http\RedirectResponse
     */
    public function create_coupon(Request $request,Coupon $couponModel)
    {
        if($couponModel->save_coupon($request->input('Coupon'))){
            return redirect('admin/coupon/coupon_list')->with('success','添加优惠券成功');
        }
    }

    /**优惠券的编辑
     * @param Request $request
     * @param Coupon $couponModel
     * @return \Illuminate\Http\RedirectResponse
     */
    public function edit_coupon(Request $request,Coupon $couponModel)
    {
        $post_data = $request ->input('Coupon');
        if(!$couponModel -> save_coupon($post_data)){
            return redirect()->back()->with('danger','编辑优惠券失败');
        }
        return redirect('admin/coupon/coupon_list')->with('success','编辑优惠券成功');
    }

    /**优惠券的删除
     * @param Request $request
     * @param Coupon $couponModel
     */
    public function delete_coupon(Request $request,Coupon $couponModel,$coupon_id=0)
    {
        if($couponModel->del_coupon($coupon_id)){
            return redirect('admin/coupon/coupon_list')->with('success','删除优惠券成功');
        }
        return redirect('admin/coupon/coupon_list')->with('error','删除优惠券失败');
    }
}
